<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-11-27
 * Time: 10:14
 */

namespace app\api\controller\v1;


use app\api\service\UserPhone;
use app\api\service\UserToken;
use app\api\validate\PhoneGet;
use app\api\validate\TokenGet;
use app\api\service\Token as TokenService;
use app\lib\exception\ParameterException;
use think\facade\Request;

class Token
{
    /**
     * 小程序获取token
     * @url /token/user
     * @http post
     * @code 小程序的code
     */
    public function getToken($code)
    {
        (new TokenGet())->goCheck();
        $ut = new UserToken($code);
        $token = $ut->get();
        return [
            'token' => $token
        ];
    }

    /**
     * 验证小程序token
     * @url /token/verify
     * @token 小程序的token
     */
    public function verifyToken($token)
    {
        if(!$token)
        {
            throw new ParameterException([
                'token' => 'token不允许为空'
            ]);
        }
        $valid = TokenService::verifyToken($token);
        return [
            'isValid' => $valid
        ];
    }

    /**
     * 获取手机号
     */
    public function getPhone()
    {
        (new PhoneGet())->goCheck();
        $up = new UserPhone();
        $post = Request::post();
        $phone = $up->get($post);
        return [
            'phone' => $phone
        ];
    }
}